<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { userInfoAPI } from '@/http/userInfo'
import {
  banUserAPI,
  getBanListAPI,
  hotUserAPI,
  searchUserAPI,
  getDepartmentListAPI,
  deleteUserAPI,
} from '@/http/admin'
import { ElMessage } from 'element-plus'
import { bus } from '@/utils/mitt'
import useUserStore from '@/store/modules/user'
// import { usePaginationStore } from '@/store/modules/pagination'
import userDetailDialog from '../components/userDetailDialog.vue'
// import pagination from '../components/pagination.vue'

// const usePaginaStore = usePaginationStore()
const userStore = useUserStore()
// userDetailDialog实例
const userDetailDialogRef = ref()
// 表格每一行的用户信息实例
const userId = ref()
// 选择器数据绑定
const select = ref()
// 搜索框数据绑定
const search = ref()
// 存放用户数据
const userData = ref()
// let isDelete = false
// const currentPage = ref(1)
// const pageSize = ref(10)
// const total = ref<number>()
// // 获取分页数据
// const returnPageListData = async () => {
//   const res = await returnPageListDataAPI(pageSize.value, currentPage.value)
//   console.log(res)
//   console.log(res.total)
//   total.value = res.total
// }
// const handleSizeChange = (newPageSize: number) => {
//   pageSize.value = newPageSize
// }
// const handleCurrentChange = (newCurrentPage: number) => {
//   currentPage.value = newCurrentPage
//   returnPageListData()
// }
// 获取用户数据
const userInfoData = async () => {
  const res = await userInfoAPI()
  userData.value = res
}
// 选择器查询
const selectOption = async () => {
  const res: any = await getDepartmentListAPI(select.value)
  if (res.length) {
    ElMessage.success('查询成功')
    userData.value = res
  } else {
    ElMessage.error('没有查到任何数据！')
    userData.value = []
  }
}
// 搜索框查询
const searchUser = async () => {
  const res = await searchUserAPI(search.value)
  if (res.length) {
    ElMessage.success('查询成功')
    userData.value = res
  } else {
    ElMessage.error(res.message)
    userData.value = []
  }
}
// 筛选冻结用户
const selectBanUser = async () => {
  const res = await getBanListAPI()
  if (res.length) {
    ElMessage.success('查询成功')
    userData.value = res
  } else {
    ElMessage.error('没有查到任何数据！')
    userData.value = []
  }
}
// 显示全部用户
const getUserInfo = () => {
  userInfoData()
}
// 点击冻结
const onClickBan = async (id: number) => {
  if (userStore.identity == '用户') return ElMessage.error('您无权进行修改！')
  let currentBanId = userData.value.filter((val: any) => val.id == id)
  if (currentBanId[0].status == 1) {
    ElMessage.error('已经是冻结状态！')
  } else {
    const res = await banUserAPI(id)
    if (res.status == 0) {
      ElMessage.success(res.message)
      userInfoData()
      location.reload()
    } else {
      ElMessage.error('冻结失败！')
    }
  }
}
// 点击解冻
const onClickHot = async (id: number) => {
  if (userStore.identity == '用户') return ElMessage.error('您无权进行修改！')
  let currentHotId = userData.value.filter((val: any) => val.id == id)
  if (currentHotId[0].status == 0) {
    ElMessage.error('已经是解冻状态！')
  } else {
    const res = await hotUserAPI(id)
    if (res.status == 0) {
      ElMessage.success(res.message)
      userInfoData()
    } else {
      ElMessage.error('解冻失败！')
    }
  }
}
const onClickDelete = async (id: number, account: string) => {
  if (userStore.identity == '用户') return ElMessage.error('您无权进行修改！')
  const res = await deleteUserAPI(id, account)
  if (res.status === 0) {
    ElMessage.success(`${res.message}`)
    userInfoData()
  }
}
const popconfirmTitle = (account: string) => {
  return `你确定要删除"${account}"吗`
}
const openUserInfo = (row: any) => {
  bus.emit('userRow', 1)
  userDetailDialogRef.value.open()
  userId.value = row
}
onMounted(() => {
  userInfoData()
})
</script>

<template>
  <el-card>
    <div class="header">
      <div class="input-select">
        <el-input
          v-model="search"
          style="width: 240px"
          size="large"
          placeholder="输入账户进行搜索"
          :suffix-icon="Search"
          @change="searchUser"
          clearable
        />
        <el-select
          v-model="select"
          placeholder="选择部门进行筛选"
          size="large"
          style="width: 240px"
          @change="selectOption"
          clearable
        >
          <el-option label="总裁办" value="总裁办" />
          <el-option label="项目部" value="项目部" />
          <el-option label="销售部" value="销售部" />
          <el-option label="组织部" value="组织部" />
          <el-option label="项目组" value="项目组" />
        </el-select>
      </div>
      <div class="btn">
        <el-button type="primary" @click="selectBanUser" plain>
          筛选冻结用户
        </el-button>
        <el-button type="primary" @click="getUserInfo" plain>
          显示全部用户
        </el-button>
      </div>
    </div>
    <div class="footer">
      <el-table
        :data="userData"
        border
        style="width: 100%"
        @row-dblclick="openUserInfo"
      >
        <el-table-column
          type="index"
          fixed
          label="序号"
          width="53"
          align="center"
        />
        <el-table-column
          prop="account"
          label="账号"
          width="150"
          align="center"
        />
        <el-table-column prop="name" label="姓名" width="120" align="center" />
        <el-table-column prop="sex" label="性别" width="53" align="center" />
        <el-table-column
          prop="identity"
          label="身份"
          width="110"
          align="center"
        />
        <el-table-column
          prop="department"
          label="部门"
          width="150"
          align="center"
        />
        <el-table-column prop="email" label="邮箱" width="180" align="center" />
        <el-table-column prop="status" label="状态" width="85" align="center">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.status === 0">正常</el-tag>
            <el-tag type="danger" v-else>冻结</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="创建时间"
          width="200"
          align="center"
        />
        <el-table-column fixed="right" label="操作">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="onClickBan(row.id)">
              冻结
            </el-button>
            <el-button type="success" size="small" @click="onClickHot(row.id)">
              解冻
            </el-button>
            <el-popconfirm
              :title="popconfirmTitle(row.account)"
              @confirm="onClickDelete(row.id, row.account)"
              width="180"
            >
              <template #reference>
                <el-button type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- <pagination
        :isDelete="isDelete"
        :search="search"
      /> -->
      <!-- <el-pagination
        style="margin-top: 15px"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 7, 10, 15]"
        background
        layout="total, prev, pager, next, -> ,sizes, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      /> -->
    </div>
    <userDetailDialog :userDataId="userId" ref="userDetailDialogRef" />
  </el-card>
</template>

<style scoped lang="scss">
.el-card {
  height: 100vh;

  .header {
    display: flex;
    justify-content: space-between;

    .input-select {
      .el-input {
        margin-right: 15px;
      }
    }
  }

  .footer {
    margin-top: 20px;
  }
}
</style>
